package com.pony.iphone.mq;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.pony.iphone.dto.QuestionMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author pony 消息去重服务
 * @create 2025-10-30 17:09
 */
@Service
@Slf4j
public class QuestionDeduplicationService {
    
    private final Cache<String, Boolean> questionCache = CacheBuilder.newBuilder()
        .expireAfterWrite(24, TimeUnit.HOURS)
        .maximumSize(10000)
        .build();

    /**
     * 判断题目是否重复
     * @param message
     * @return
     */
    public boolean isDuplicate(QuestionMessage message) {
        String deduplicationKey = message.getDeduplicationKey();

        Boolean existing = questionCache.getIfPresent(deduplicationKey);
        if (existing != null) {
            log.info("题目重复，已跳过: {}", message.getQuestionContent());
            return true;
        }

        questionCache.put(deduplicationKey, true);
        return false;
    }
}